<template>
	<view class="container">
		<view class="header">
			<m-header class="m-header" :list="adDatas"></m-header>
			<view class="container-header">
				<view class="left">
					<u-icon name="map-fill" color="#fff" size="32"></u-icon>
					<text @click="cityActive = true">{{area2}}</text>
				</view>
				<view class="center">
					<u-icon class="search" name="search" color="#fff" size="36"></u-icon>
					<u-input placeholder="请输入关键词" v-model="form.searchKey" type="text" :border="true" :height="height" placeholderStyle="color:#fff" v-on:input="watchKey"/>
				</view>
				<view class="right">
					<view class="switch" @click="handleSwitch">
						<view class="item" :class="{'select':!active}">全部</view>
						<view class="item" :class="{'select':active}">匹配</view>
					</view>
				</view>
			</view>
		</view>
		<view>
			<yd-classification :list="lists" :index="index" :selectedColor="color" :type1LineHeight="lineheight1" :type1Size="size1" :type2Size="size2" :type3Size="size3" :type1Height="heigth1" @selectType="handleClickSwitch" @onRefresh="onRefresh" @getMoreLists="getMoreLists" @handleItemClick="handleItemClick"></yd-classification>
		</view>
		<city-select v-model="cityActive" @city-change="cityChange"></city-select>
	</view>
</template>

<script>
	import classifyData from "@/common/classify.data.js";
	import mHeader from '@/components/m-header/m-header.vue';
	import citySelect from './components/u-city-select.vue';
	export default {
		components:{
			mHeader,
			citySelect
		},
		data() {
			return {				
				type:[],
				index: 0,
				color: "#0D1D36",
				size1: "16px",
				size2: "16px",
				size3: "16px",
				heigth1: "110rpx",
				lineheight1: "110rpx",
				adDatas:[],
				area2:'全国',
				cityActive: false,
				active: false,
				value: '',
				height: 64,
				tabbar: classifyData,
				scrollTop: 0, //tab标题的滚动条位置
				current: 0, // 预设当前项的值
				menuHeight: 0, // 左边菜单的高度
				menuItemHeight: 0, // 左边菜单item的高度
				form:{
					pageNum:1,
					pageSize:10,
					type:'',
					isPp:'',
					area:'',
					searchKey:''
				},
				loadStatus: 'loadmore',
				lists:[]
			};
		},
		watch: {
		},
		onLoad() {
			this.findfetchAd('1');
			this.findextraIndex();
			this.windows_height = uni.getSystemInfoSync().windowHeight;
		},
		mounted() {			
			this.getList();
		},
		methods: {	
　　　　　　// 自定义下拉刷新被触发
			onRefresh(ref) {
				if (this._freshing) return;
				this._freshing = true;
				if (!this.triggered) this.triggered = true; //界面下拉触发，triggered可能不是true，要设为true
				const isRefresh = ref;
				this.getList();
				this.triggered = false;
				this._freshing = false;
				this.form.pageNum = 1;
			},
　　　　　　// 滚动到底部 加载更多
			getMoreLists(pageNum){
				this.form.pageNum = pageNum;
				console.log("滚动底部");
				this.getList(this.form.pageNum);
			},
			select(n1, n2, n3){
				console.log("点击传来的值为",n1,n2,n3)
				console.log("点击的值为",this.type[n1].child[n2].child[n3].name)
			},		
				async findextraIndex(){
					let result = await this.$apis.extraIndex();
					this.endVal = this.endVal + result.data;
				},
			async findfetchAd(type){
				let result = await this.$apis.fetchAd({type:type});
				this.adDatas = result.data;
			},
			watchKey(e){
				console.log(e)
				this.form.pageNum = 1;
				this.getList();
			},
			cityChange(e) {
				let address = e.province.label
				this.area2 = e.province.label
				if(e.city){
					address = address + '-' + e.city.label;
					this.area2 = e.city.label
				}
				if(e.area){
					address = address + '-' + e.area.label;
					this.area2 = e.area.label
				}
				this.form.area = address
				this.form.pageNum = 1;
				this.getList();
				console.log('address',e,address)
			},
			async handleItemClick(current,item){
				let type =this.tabbar[this.current].type;
				let data = await this.$apis.addBusinessFootprint(item);
				uni.navigateTo({
					url:'./teamDetail?type='+type+'&row='+encodeURIComponent(JSON.stringify(item))
				})
			},
			handleSwitch() {
				this.active = !this.active;
				var type = this.tabbar[this.current].type;
				this.form = {
					pageNum:1,
					pageSize:10,
					type:type,
					isPp:!this.active,
					area:this.form.area,
					searchKey:''
				};
				this.loadStatus = 'loadmore';
				this.getList();
			},
			handleClickSwitch(current) {
				var type = this.tabbar[this.current].type;
				this.current = current;
				this.form = {
					pageNum:1,
					pageSize:10,
					type:type,
					isPp:!this.active,
					area:this.form.area,
					searchKey:''
				};
				this.loadStatus = 'loadmore';
				this.getList();
			},
			async getList(page){
				console.log("current",this.current);
				if(this.tabbar[this.current] != undefined) this.form.type = this.tabbar[this.current].type;
				this.form.isPp = this.active;
				let data = await this.$apis.followToday(this.form);
				if(data.code === 200){
					if(data.data.content.length<data.data.content.size){
						this.loadStatus = "nomore";
					}
					if(page){
						this.lists =this.lists.concat(data.data.content);
					}else{
						this.lists = data.data.content;
					}
					this.$forceUpdate()
				}
			},			
			getImg() {
				return Math.floor(Math.random() * 35);
			},
			// 点击左边的栏目切换
			async swichMenu(index) {
				console.log(index)
				if (index == this.current) return;
				this.current = index;
				this.form = {
					pageNum:1,
					pageSize:10,
					type:'',
					isPp:'',
					area:'',
					searchKey:''
				};
				this.loadStatus = 'loadmore';
				this.getList();
				// 如果为0，意味着尚未初始化
				if (this.menuHeight == 0 || this.menuItemHeight == 0) {
					await this.getElRect('menu-scroll-view', 'menuHeight');
					await this.getElRect('u-tab-item', 'menuItemHeight');
				}
				// 将菜单菜单活动item垂直居中
				this.scrollTop = index * this.menuItemHeight + this.menuItemHeight / 2 - this.menuHeight / 2;
			},
			// 获取一个目标元素的高度
			getElRect(elClass, dataVal) {
				new Promise((resolve, reject) => {
					const query = uni.createSelectorQuery().in(this);
					query.select('.' + elClass).fields({
						size: true
					}, res => {
						// 如果节点尚未生成，res值为null，循环调用执行
						if (!res) {
							setTimeout(() => {
								this.getElRect(elClass);
							}, 10);
							return;
						}
						this[dataVal] = res.height;
					}).exec();
				})
			}

		}
	}
</script>

<style lang="scss" scoped>
	page{
	}
	.m-header {background: $uni-bg-color-ad;}
	.container {
		width: 100%;
		.container-header {
			width: 100%;
			height: 100upx;
			background-color: #7E8DFF;
			padding: 18upx 20upx;
			display: flex;
			justify-content: space-between;

			.center {
				height: 64upx;
				flex: 1;
				margin: 0 27upx;
				position: relative;

				.search {
					position: absolute;
					height: 64upx;
					left: 20upx;
				}

				.u-input {
					height: 64upx;
					line-height: 64upx;
					border-radius: 32upx;
					color: #FFFFFF;
					font-size: 26upx;

					/deep/ .u-input__input {
						height: 64upx;
						line-height: 64upx;
						color: #fff;
						padding: 0 20upx 0 50upx;
					}
				}
			}

			.right {
				height: 64upx;

				.switch {
					margin-top: 7upx;
					height: 50upx;
					width: 140upx;
					background: rgba(255, 255, 255, 0.4);
					border: 1upx solid #FFFFFF;
					border-radius: 25upx;
					display: flex;

					.item {
						width: 70upx;
						line-height: 46upx;
						text-align: center;
						font-size: 22upx;
						color: #fffffe;
					}

					.select {
						background-color: #FFFFFF;
						color: #FFAD1F;
						border-radius: 23upx;
					}
				}
			}

			.left {
				line-height: 64upx;
				height: 100%;

				text {
					color: #fff;
					margin-left: 10upx;
					font-size: 26upx;
					line-height: 26upx;
				}
			}
		}

		.u-wrap {
			height: calc(100vh);
			/* #ifdef H5 */
			height: calc(100vh - var(--window-top));
			/* #endif */
			display: flex;
			flex-direction: column;
		}

		.u-search-box {
			padding: 18rpx 30rpx;
		}

		.u-menu-wrap {
			flex: 1;
			display: flex;
			overflow: hidden;
		}

		.u-search-inner {
			background-color: rgb(234, 234, 234);
			border-radius: 100rpx;
			display: flex;
			align-items: center;
			padding: 10rpx 16rpx;
		}

		.u-search-text {
			font-size: 26rpx;
			color: $u-tips-color;
			margin-left: 10rpx;
		}

		.u-tab-view {
			width: 200rpx;
			height: 100%;
		}

		.u-tab-item {
			height: 110rpx;
			background: #f6f6f6;
			box-sizing: border-box;
			display: flex;
			align-items: center;
			justify-content: center;
			font-size: 26rpx;
			color: #444;
			font-weight: 400;
			line-height: 1;
		}

		.u-tab-item-active {
			position: relative;
			color: #000;
			font-size: 30rpx;
			font-weight: 600;
			background: #fff;
		}

		.u-tab-item-active::before {
			content: "";
			position: absolute;
			border-left: 4px solid $u-type-primary;
			height: 32rpx;
			left: 0;
			top: 39rpx;
		}

		.u-tab-view {
			height: 100%;
		}

		.right-box {
			background-color: rgb(250, 250, 250);
		}

		.page-view {
			padding: 16rpx;
		}

		.class-item {
			margin-bottom: 30rpx;
			background-color: #fff;
			padding: 16rpx;
			border-radius: 8rpx;
		}

		.item-title {
			width:350rpx;
			font-size: 26rpx;
			color: $u-main-color;
			font-weight: bold;
		}

		.item-menu-name {
			font-weight: normal;
			font-size: 24rpx;
			color: $u-main-color;
		}

		.item-container {
			display: flex;
			flex-wrap: wrap;
		}

		.thumb-box {
			width: 100%;
			display: flex;
			// align-items: center;
			// justify-content: center;
			flex-direction: column;
			padding: 33upx 33upx 0 33upx;
			border-bottom: #DADFED solid 1upx;
			.item-line{
				margin-bottom: 34upx;
				.name{
					color: #9DA2B0;
					font-size: 28upx;
				}
				.title{
					color: #4B9CFF;
					font-size: 28upx;
				}
				.value{
					color: #0D1D36;
					font-size: 28upx;
					margin-left: 60upx;
				}
			}
		}
		.thumb-box:last-child{border:none;}

		.item-menu-image {
			width: 120rpx;
			height: 120rpx;
		}
	}
</style>
